

use serde::Deserialize;
use shq_common::prelude::{get_app_config, get_ch_pool};

#[derive(Debug, Deserialize)]
pub struct ImportConfigs {
    configs: Vec<ImportConfig>,
}
impl ImportConfigs {
    pub fn new(configs: Vec<ImportConfig>) -> Self {
        Self { configs }
    }
    pub fn find_by_file_name(&self, file_name: &str) -> Option<&ImportConfig> {
        self.configs
            .iter()
            .find(|c| file_name.contains(c.file_name.as_str()))
    }
}
#[derive(Debug, Clone, Deserialize)]
pub struct ImportConfig {
    pub file_name: String,
    #[serde(default)]
    pub param_name: String,
    #[serde(default)]
    pub param_type: ParamType,
    #[serde(default)]
    pub date_after: String,
    #[serde(default)]
    pub rept_names: Vec<String>,
    pub configs: Vec<Config>,
}
impl ImportConfig{
     pub fn get_config_name(&self, pval: u32) -> Option<&str> {
        if self.param_name.is_empty() {
            if self.configs.len()==0{
                return None;
            }
            return Some(self.configs[0].conf_name.as_str());
        }
        self.configs
            .iter()
            .find(|c| pval >= c.start && pval <= c.end)
            .map(|c| c.conf_name.as_str())
    }
}
#[derive(Debug, Clone, Deserialize)]
pub struct Config {
    pub conf_name: String,
    #[serde(default)]
    pub start: u32,
    #[serde(default)]
    pub end: u32,
}

#[derive(Debug, Copy, Clone, Deserialize)]
pub enum ParamType {
    Quarter,
    Month,
}

impl Default for ParamType {
    fn default() -> Self {
        ParamType::Month
    }
}

